package axter.testrecyclerview;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.List;

public class MyAdapter2 extends RecyclerView.Adapter<MyAdapter2.ViewHolder> {

	// 数据集
	private List<ImageItem> mDataset;

	public MyAdapter2(List<ImageItem> dataset) {
		super();
		mDataset = dataset;
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
		// 创建一个View，简单起见直接使用系统提供的布局，就是一个TextView
//		View view = View.inflate(viewGroup.getContext(), android.R.layout.simple_list_item_1, null);
		// 创建一个ViewHolder
		ViewHolder holder = new ViewHolder(new ImageView(viewGroup.getContext()));

		Log.i("position", viewType + "");
		return holder;
	}

	@Override
	public void onBindViewHolder(ViewHolder viewHolder, int i) {
		// 绑定数据到ViewHolder上
		viewHolder.mTextView.setBackgroundResource(R.mipmap.ic_launcher);
		RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) viewHolder.mTextView.getLayoutParams();
		if(params == null){
			params = new RecyclerView.LayoutParams(mDataset.get(i).w,mDataset.get(i).h);
		}else{
			params.width = mDataset.get(i).w;
			params.height = mDataset.get(i).h;
		}
		viewHolder.mTextView.setLayoutParams(params);
	}

	@Override
	public int getItemCount() {
		return mDataset.size();
	}

	public static class ViewHolder extends RecyclerView.ViewHolder {

		public ImageView mTextView;

		public ViewHolder(View itemView) {
			super(itemView);
			mTextView = (ImageView) itemView;
		}
	}
}